home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / tcl / tests / format.test < prev    next >
Text File  |  1992-11-06  |  12KB  |  332 lines

  1. # Commands covered:  format
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright 1991 Regents of the University of California
  8. # Permission to use, copy, modify, and distribute this
  9. # software and its documentation for any purpose and without
  10. # fee is hereby granted, provided that this copyright notice
  11. # appears in all copies.  The University of California makes no
  12. # representations about the suitability of this software for any
  13. # purpose.  It is provided "as is" without express or implied
  14. # warranty.
  15. #
  16. # $Header: /user6/ouster/tcl/tests/RCS/format.test,v 1.10 92/06/17 09:00:08 ouster Exp $ (Berkeley)
  17.  
  18. if {[string compare test [info procs test]] == 1} then {source defs}
  19.  
  20. # The following code is needed because some versions of SCO Unix have
  21. # a round-off error in sprintf which would cause some of the tests to
  22. # fail.  Someday I hope this code shouldn't be necessary (code added
  23. # 9/9/91).
  24.  
  25. set roundOffBug 0
  26. if {"[format %7.1e  68.514]" == "6.8e+01"} {
  27.     puts stdout "Note: this system has a sprintf round-off bug, some tests skipped\n"
  28.     set roundOffBug 1
  29. }
  30.  
  31. test format-1.1 {integer formatting} {
  32.     format "%*d %d %d %d" 6 34 16923 -12 -1
  33. } {    34 16923 -12 -1}
  34. if $atBerkeley {
  35.     test format-1.2 {integer formatting} {
  36.     format "%4d %4d %4d %4d %d %#x %#X" 6 34 16923 -12 -1 0 0
  37.     } {   6   34 16923  -12 -1 0 0}
  38. }
  39.  
  40. # %u output depends on word length, so don't run these tests except
  41. # at Berkeley, where word length is known.
  42.  
  43. if $atBerkeley {
  44.     test format-1.3 {integer formatting} {
  45.     format "%4u %4u %4u %4u %d %#o" 6 34 16923 -12 -1 0
  46.     } {   6   34 16923 4294967284 -1 0}
  47. }
  48. test format-1.4 {integer formatting} {
  49.     format "%-4d %-4d %-4d %-4ld" 6 34 16923 -12 -1
  50. } {6    34   16923 -12 }
  51. test format-1.5 {integer formatting} {
  52.     format "%04d %04d %04d %04d" 6 34 16923 -12 -1
  53. } {0006 0034 16923 -012}
  54. test format-1.6 {integer formatting} {
  55.     format "%00*d" 6 34
  56. } {000034}
  57.  
  58. # Printing negative numbers in hex or octal format depends on word
  59. # length; only run at Berkeley where word length is known.
  60.  
  61. if $atBerkeley {
  62.     test format-1.7 {integer formatting} {
  63.     format "%4x %4x %4x %4x" 6 34 16923 -12 -1
  64.     } {   6   22 421b fffffff4}
  65.     test format-1.8 {integer formatting} {
  66.     format "%#x %#X %#X %#x" 6 34 16923 -12 -1
  67.     } {0x6 0X22 0X421B 0xfffffff4}
  68.     test format-1.9 {integer formatting} {
  69.     format "%#20x %#20x %#20x %#20x" 6 34 16923 -12 -1
  70.     } {                 0x6                 0x22               0x421b           0xfffffff4}
  71.     test format-1.10 {integer formatting} {
  72.     format "%-#20x %-#20x %-#20x %-#20x" 6 34 16923 -12 -1
  73.     } {0x6                  0x22                 0x421b               0xfffffff4          }
  74.     test format-1.11 {integer formatting} {
  75.     format "%-#20o %#-20o %#-20o %#-20o" 6 34 16923 -12 -1
  76.     } {06                   042                  041033               037777777764        }
  77. }
  78.  
  79. test format-2.1 {string formatting} {
  80.     format "%s %s %c %s" abcd {This is a very long test string.} 120 x
  81. } {abcd This is a very long test string. x x}
  82. test format-2.2 {string formatting} {
  83.     format "%20s %20s %20c %20s" abcd {This is a very long test string.} 120 x
  84. } {                abcd This is a very long test string.                    x                    x}
  85. test format-2.3 {string formatting} {
  86.     format "%.10s %.10s %c %.10s" abcd {This is a very long test string.} 120 x
  87. } {abcd This is a  x x}
  88. test format-2.4 {string formatting} {
  89.     format "%s %s %% %c %s" abcd {This is a very long test string.} 120 x
  90. } {abcd This is a very long test string. % x x}
  91.  
  92. test format-3.1 {e and f formats} {
  93.     format "%e %e %e %e" 34.2e12 68.514 -.125 -16000. .000053
  94. } {3.420000e+13 6.851400e+01 -1.250000e-01 -1.600000e+04}
  95. test format-3.2 {e and f formats} {
  96.     format "%20e %20e %20e %20e" 34.2e12 68.514 -.125 -16000. .000053
  97. } {        3.420000e+13         6.851400e+01        -1.250000e-01        -1.600000e+04}
  98. if {!$roundOffBug} {
  99.     test format-3.3 {e and f formats} {
  100.     format "%.1e %.1e %.1e %.1e" 34.2e12 68.514 -.126 -16000. .000053
  101.     } {3.4e+13 6.9e+01 -1.3e-01 -1.6e+04}
  102.     test format-3.4 {e and f formats} {
  103.     format "%020e %020e %020e %020e" 34.2e12 68.514 -.126 -16000. .000053
  104.     } {000000003.420000e+13 000000006.851400e+01 -00000001.260000e-01 -00000001.600000e+04}
  105.     test format-3.5 {e and f formats} {
  106.     format "%7.1e %7.1e %7.1e %7.1e" 34.2e12 68.514 -.126 -16000. .000053
  107.     } {3.4e+13 6.9e+01 -1.3e-01 -1.6e+04}
  108.     test format-3.6 {e and f formats} {
  109.     format "%f %f %f %f" 34.2e12 68.514 -.125 -16000. .000053
  110.     } {34200000000000.000000 68.514000 -0.125000 -16000.000000}
  111. }
  112. test format-3.7 {e and f formats} {
  113.     format "%.4f %.4f %.4f %.4f %.4f" 34.2e12 68.514 -.125 -16000. .000053
  114. } {34200000000000.0000 68.5140 -0.1250 -16000.0000 0.0001}
  115. test format-3.8 {e and f formats} {
  116.     format "%.4e %.5e %.6e" -9.99996 -9.99996 9.99996
  117. } {-1.0000e+01 -9.99996e+00 9.999960e+00}
  118. test format-3.9 {e and f formats} {
  119.     format "%.4f %.5f %.6f" -9.99996 -9.99996 9.99996
  120. } {-10.0000 -9.99996 9.999960}
  121. test format-3.10 {e and f formats} {
  122.     format "%20f %-20f %020f" -9.99996 -9.99996 9.99996
  123. } {           -9.999960 -9.999960            0000000000009.999960}
  124. test format-3.11 {e and f formats} {
  125.     format "%-020f %020f" -9.99996 -9.99996 9.99996
  126. } {-9.999960            -000000000009.999960}
  127. test format-3.12 {e and f formats} {
  128.     format "%.0e %#.0e" -9.99996 -9.99996 9.99996
  129. } {-1e+01 -1.e+01}
  130. test format-3.13 {e and f formats} {
  131.     format "%.0f %#.0f" -9.99996 -9.99996 9.99996
  132. } {-10 -10.}
  133. test format-3.14 {e and f formats} {
  134.     format "%.4f %.5f %.6f" -9.99996 -9.99996 9.99996
  135. } {-10.0000 -9.99996 9.999960}
  136. test format-3.15 {e and f formats} {
  137.     format "%3.0f %3.0f %3.0f %3.0f" 1.0 1.1 1.01 1.001
  138. } {  1   1   1   1}
  139. test format-3.16 {e and f formats} {
  140.     format "%3.1f %3.1f %3.1f %3.1f" 0.0 0.1 0.01 0.001
  141. } {0.0 0.1 0.0 0.0}
  142.  
  143. test format-4.1 {g-format} {
  144.     format "%.3g" 12341.0
  145. } {1.23e+04}
  146. test format-4.2 {g-format} {
  147.     format "%.3G" 1234.12345
  148. } {1.23E+03}
  149. test format-4.3 {g-format} {
  150.     format "%.3g" 123.412345
  151. } {123}
  152. test format-4.4 {g-format} {
  153.     format "%.3g" 12.3412345
  154. } {12.3}
  155. test format-4.5 {g-format} {
  156.     format "%.3g" 1.23412345
  157. } {1.23}
  158. test format-4.6 {g-format} {
  159.     format "%.3g" 1.23412345
  160. } {1.23}
  161. test format-4.7 {g-format} {
  162.     format "%.3g" .123412345
  163. } {0.123}
  164. test format-4.8 {g-format} {
  165.     format "%.3g" .012341
  166. } {0.0123}
  167. test format-4.9 {g-format} {
  168.     format "%.3g" .0012341
  169. } {0.00123}
  170. test format-4.10 {g-format} {
  171.     format "%.3g" .00012341
  172. } {0.000123}
  173. test format-4.11 {g-format} {
  174.     format "%.3g" .00001234
  175. } {1.23e-05}
  176. test format-4.12 {g-format} {
  177.     format "%.4g" 9999.6
  178. } {1e+04}
  179. test format-4.13 {g-format} {
  180.     format "%.4g" 999.96
  181. } {1000}
  182. test format-4.14 {g-format} {
  183.     format "%.3g" 1.0
  184. } {1}
  185. test format-4.15 {g-format} {
  186.     format "%.3g" .1
  187. } {0.1}
  188. test format-4.16 {g-format} {
  189.     format "%.3g" .01
  190. } {0.01}
  191. test format-4.17 {g-format} {
  192.     format "%.3g" .001
  193. } {0.001}
  194. test format-4.19 {g-format} {
  195.     format "%.3g" .00001
  196. } {1e-05}
  197. test format-4.20 {g-format} {
  198.     format "%#.3g" 1234.0
  199. } {1.23e+03}
  200. test format-4.21 {g-format} {
  201.     format "%#.3G" 9999.5
  202. } {1.00E+04}
  203.  
  204. test format-5.1 {floating-point zeroes} {
  205.     format "%e %f %g" 0.0 0.0 0.0 0.0
  206. } {0.000000e+00 0.000000 0}
  207. test format-5.2 {floating-point zeroes} {
  208.     format "%.4e %.4f %.4g" 0.0 0.0 0.0 0.0
  209. } {0.0000e+00 0.0000 0}
  210. test format-5.3 {floating-point zeroes} {
  211.     format "%#.4e %#.4f %#.4g" 0.0 0.0 0.0 0.0
  212. } {0.0000e+00 0.0000 0.000}
  213. test format-5.4 {floating-point zeroes} {
  214.     format "%.0e %.0f %.0g" 0.0 0.0 0.0 0.0
  215. } {0e+00 0 0}
  216. test format-5.5 {floating-point zeroes} {
  217.     format "%#.0e %#.0f %#.0g" 0.0 0.0 0.0 0.0
  218. } {0.e+00 0. 0.}
  219. test format-5.6 {floating-point zeroes} {
  220.     format "%3.0f %3.0f %3.0f %3.0f" 0.0 0.0 0.0 0.0
  221. } {  0   0   0   0}
  222. test format-5.7 {floating-point zeroes} {
  223.     format "%3.0f %3.0f %3.0f %3.0f" 1.0 1.1 1.01 1.001
  224. } {  1   1   1   1}
  225. test format-5.8 {floating-point zeroes} {
  226.     format "%3.1f %3.1f %3.1f %3.1f" 0.0 0.1 0.01 0.001
  227. } {0.0 0.1 0.0 0.0}
  228.  
  229. test format-6.1 {various syntax features} {
  230.     format "%*.*f" 12 3 12.345678901
  231. } {      12.346}
  232. test format-6.2 {various syntax features} {
  233.     format "%0*.*f" 12 3 12.345678901
  234. } {00000012.346}
  235.  
  236. test format-7.1 {error conditions} {
  237.     catch format
  238. } 1
  239. test format-7.2 {error conditions} {
  240.     catch format msg
  241.     set msg
  242. } {wrong # args: should be "format formatString ?arg arg ...?"}
  243. test format-7.3 {error conditions} {
  244.     catch {format %*d}
  245. } 1
  246. test format-7.4 {error conditions} {
  247.     catch {format %*d} msg
  248.     set msg
  249. } {not enough arguments for all format specifiers}
  250. test format-7.5 {error conditions} {
  251.     catch {format %*.*f 12}
  252. } 1
  253. test format-7.6 {error conditions} {
  254.     catch {format %*.*f 12} msg
  255.     set msg
  256. } {not enough arguments for all format specifiers}
  257. test format-7.7 {error conditions} {
  258.     catch {format %*.*f 12 3}
  259. } 1
  260. test format-7.8 {error conditions} {
  261.     catch {format %*.*f 12 3} msg
  262.     set msg
  263. } {not enough arguments for all format specifiers}
  264. test format-7.9 {error conditions} {
  265.     list [catch {format %*d x 3} msg] $msg
  266. } {1 {expected integer but got "x"}}
  267. test format-7.10 {error conditions} {
  268.     list [catch {format %*.*f 2 xyz 3} msg] $msg
  269. } {1 {expected integer but got "xyz"}}
  270. test format-7.11 {error conditions} {
  271.     catch {format %d 2a}
  272. } 1
  273. test format-7.12 {error conditions} {
  274.     catch {format %d 2a} msg
  275.     set msg
  276. } {expected integer but got "2a"}
  277. test format-7.13 {error conditions} {
  278.     catch {format %c 2x}
  279. } 1
  280. test format-7.14 {error conditions} {
  281.     catch {format %c 2x} msg
  282.     set msg
  283. } {expected integer but got "2x"}
  284. test format-7.15 {error conditions} {
  285.     catch {format %f 2.1z}
  286. } 1
  287. test format-7.16 {error conditions} {
  288.     catch {format %f 2.1z} msg
  289.     set msg
  290. } {expected floating-point number but got "2.1z"}
  291. test format-7.17 {error conditions} {
  292.     catch {format ab%}
  293. } 1
  294. test format-7.18 {error conditions} {
  295.     catch {format ab% 12} msg
  296.     set msg
  297. } {format string ended in middle of field specifier}
  298. test format-7.19 {error conditions} {
  299.     catch {format %q x}
  300. } 1
  301. test format-7.20 {error conditions} {
  302.     catch {format %q x} msg
  303.     set msg
  304. } {bad field specifier "q"}
  305. test format-7.21 {error conditions} {
  306.     catch {format %d}
  307. } 1
  308. test format-7.22 {error conditions} {
  309.     catch {format %d} msg
  310.     set msg
  311. } {not enough arguments for all format specifiers}
  312.  
  313. test format-8.1 {long result} {
  314.     set a {1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}
  315.     format {1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG %s %s %s} $a $a $a
  316. } {1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}
  317.  
  318. if $atBerkeley {
  319.     test format-9.1 {"h" format specifier} {
  320.     format %hd 0xffff
  321.     } -1
  322.     test format-9.2 {"h" format specifier} {
  323.     format %hx 0x10fff
  324.     } fff
  325.     test format-9.3 {"h" format specifier} {
  326.     format %hd 0x10000
  327.     } 0
  328.     test format-9.4 {"h" format specifier} {
  329.     format %hD 0x10000
  330.     } 65536
  331. }
  332.